package com.jzdjt.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.PaperEntity;
import com.jzdjt.tool.DBUtil;
import com.jzdjt.tool.DESHelp;

public class UpdateUserAmountServlet extends HttpServlet {

	public UpdateUserAmountServlet() {
		super();
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		 
		boolean result =false;

		PrintWriter out = response.getWriter();// 回应请求

		out.write(result + "");
		out.flush();
		out.close();
		System.out.println(result);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doGet(request, response);
		// 设置客户端的解码方式为utf-8
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");

		// 根据标示名获取JSP文件中表单所包含的参数
		String stuid = request.getParameter("stuid");
		String amount = request.getParameter("amount");
		System.out.println("update student balance:"+stuid+"-"+amount);

		String result = updateSql(stuid, amount);
		PrintWriter out = response.getWriter();// 回应请求
		out.write(result + "");
		out.flush();
		out.close();
		System.out.println("update student balance:"+result);
	}

	private String updateSql(String stuid, String amount) {
		float balance = 0.0f;
		try {
			Date d = new Date(); // 获取当前系统的时间
			// 格式化日期
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			String updateTime = sdf.format(d); // 转为字符串
			String payType = "app 充值";
			DBUtil dbUtil = new DBUtil();
			
			String sql = "select balance from student where studentID='"+stuid+"'";
			
 	        List<String>  balancelist = dbUtil.QueryFromAmount(sql);
 	        DESHelp desHelp = new DESHelp("jxncgmjy");
 	        if(balancelist!=null&&balancelist.size()>0){
				if(balancelist.get(0)!=null&&!balancelist.get(0).trim().equals("")){
 					if(balancelist.get(0).contains("."))
 						balance = Float.parseFloat(balancelist.get(0));
 					else{
 						System.out.println("余额解密："+"("+balancelist.get(0)+")"+desHelp.decrypt(balancelist.get(0)));
 						balance =  Float.parseFloat(desHelp.decrypt(balancelist.get(0)));

 					}
				}
				else{
					balance = 0.0f;
				}
			}
	 	    System.out.println("update student balance amount:"+amount);
	        
	 	    if(amount!=null&&amount.length()>4){
				System.out.println("amount 解密："+"("+amount+")"+desHelp.decrypt(amount));

	 	    	float amountfloat = Float.parseFloat(desHelp.decrypt(amount));
	 	    	balance += amountfloat;
	 	    }
	 	    String amountstr = desHelp.encrypt(balance+"");
			String updateAmountSql = "update student set balance = '"+amountstr+"' where studentID = '"+stuid+"'";
			System.out.println("update student balance update sql:"+updateAmountSql);
			dbUtil.update(updateAmountSql);
			String updateChargeSql = "insert into recharge(studentID,feeAmount,feeType,feeData,result,productID) values('"+stuid+"','"+amount+"','"+payType+"','"+updateTime+"','充值成功','1')";
			dbUtil.update(updateChargeSql);
			return amountstr+"";
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
			//e.printStackTrace();
			return "0.0";
		}  
	}

}